﻿using System;
using System.Collections.Generic;
using SrBBSPro.Entities;
using SrBBSPro.BaseClasses;

namespace SrBBSPro.Services
{
    public class NoteService: BaseDoubleService<HolidayNote> 
    {
        public List<HolidayNote> GetNoteList(long hid, string name, string depart, out int count, int page)
        {

            var li = DbBase.Query<HolidayNote>()
                .Include(h => h.User)
                .Where(h => h.Id == hid).ToList();

            if (!string.IsNullOrEmpty(name))
            {
                li = li.FindAll(h => h.User.StaffName.Contains(name));
            }
            if (!string.IsNullOrEmpty(depart))
            {
                li = li.FindAll(h => h.User.Depart == depart);
            }
            
            return GetLimitList(li,out count, page);
        }

        /// <summary>
        /// 获取用户的休假信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public HolidayNote GetHolidayById(long hid, string userId)
        {
            var res = DbBase.Query<HolidayNote>().Include(h => h.HolidayCount)
                .Include(h => h.User)
                .Where(h => h.Id == hid)
                .Where(h => h.No == userId)
                .SingleOrDefault();
            if(res == null)
            {
                res = new HolidayNote()
                {
                    Id = hid,
                    No= userId,
                    User=GetUserInfo(userId)
                };
                var count = DbBase.SingleOrDefaultById<HolidayCount>(hid);
                res.EndDate = count.EndDate;
                res.StartDate = count.StartDate;
                res.HolidayCount = count;
            }
            return res;
        }

    }
}